package cn.itsource.ldj.base.service.impl;

import cn.itsource.ldj.base.mapper.BaseMapper;
import cn.itsource.ldj.base.service.IBaseService;
import cn.itsource.ldj.org.query.BaseQuery;
import cn.itsource.ldj.util.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;


    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    /* 分页和高级查询 */
    @Override
    public QueryResult<T> queryPage(BaseQuery baseQuery) {
        // 查询总条数
        Integer total = baseMapper.findCount(baseQuery);

        // 查询分页数据
        List<T> data = baseMapper.queryPage(baseQuery);
        return new QueryResult<>(total,data);
    }

    /* 批量删除 */
    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {

        baseMapper.batchRemove(ids);
    }
}
